package com.wangbo.box.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合模式
 *
 * 在需要表示一个对象整体与部分的层次结构的场合。
 * 要求对用户隐藏组合对象与单个对象的不同，用户可以用统一的接口使用组合结构中的所有对象的场合。
 */
public class CompositePattern {

    interface Subject{
        void show();
        void add(Subject subject);
    }

    static class SubjectInstance implements Subject{

        private String name;

        @Override
        public void add(Subject subject){

        }

        @Override
        public void show() {
            System.out.println("subject instance " + name);
        }

        public SubjectInstance(String name) {
            this.name = name;
        }
    }

    static class Subjects implements Subject{
        List<Subject> subjects = new ArrayList<>();
        @Override
        public void add(Subject subject){
            subjects.add(subject);
        }

        @Override
        public void show() {
            subjects.forEach(Subject::show);
        }
    }

    public static void main(String[] args) {
        Subject subjectsA = new Subjects();
        Subject subjectsB = new Subjects();
        Subject subjectA = new SubjectInstance("A");
        Subject subjectB = new SubjectInstance("B");
        Subject subjectC = new SubjectInstance("C");
        subjectsA.add(subjectA);
        subjectsA.add(subjectsB);
        subjectsB.add(subjectB);
        subjectsB.add(subjectC);
        subjectsA.show();
    }
}
